package com.example.service;

import cn.hutool.core.collection.CollUtil;
import com.example.entity.Thumb;
import com.example.mapper.ThumbMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论信息表业务处理
 **/
@Service
public class ThumbService {

    @Resource
    private ThumbMapper thumbMapper;

    /**
     * 新增
     */
    public void add(Thumb thumb) {
        thumbMapper.insert(thumb);
    }

    /**
     * 后台删除
     */
    public void deleteById(Integer id) {
        thumbMapper.deleteById(id);
    }

    /**
     * 前台递归删除
     */
    public void deepDelete(Integer pid) {
        thumbMapper.deleteById(pid);
        List<Thumb> children = thumbMapper.selectByPid(pid);
        if (CollUtil.isNotEmpty(children)) {
            for (Thumb thumb : children) {
                pid = thumb.getId();
                this.deepDelete(pid);
            }
        }
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            thumbMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Thumb thumb) {
        thumbMapper.updateById(thumb);
    }

    /**
     * 根据ID查询
     */
    public Thumb selectById(Integer id) {
        return thumbMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Thumb> selectAll(Thumb thumb) {
        return thumbMapper.selectAll(thumb);
    }

    /**
     * 分页查询
     */
    public PageInfo<Thumb> selectPage(Thumb thumb, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Thumb> list = thumbMapper.selectAll(thumb);
        return PageInfo.of(list);
    }

}